package live.betterman.api.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Entry {
    //    private LinkedList<Prescription> runningTask;
    // 生成一个产线
    private static final Line normalLine = new Line();
    // 模拟工单
    private static final List<LinkedList<Station>> pathList = new ArrayList<>();
    // 工位
    private static List<Station> stations;

    private static void init() {
        // 生成N个工位
        stations = new LinkedList<>();
        for (int i = 0; i < 6; i++) {
            stations.add(new Station(i));
        }

        // 生成4个工单, 数字是要到达的工位
        List<List<Integer>> codes = Arrays.asList(Arrays.asList(0, 3, 4, 5), Arrays.asList(0, 2, 3), Arrays.asList(1, 4, 5), Arrays.asList(0, 2, 5));
//        List<List<Integer>> codes = Arrays.asList(Arrays.asList(0, 4, 5), Arrays.asList(1, 4));

        for (List<Integer> code : codes) {
            LinkedList<Station> path = new LinkedList<>();
            for (Integer i : code) {
                path.add(stations.get(i));
            }
            pathList.add(path);
        }
    }

    public static void main(String[] args) {
        init();

        try {
            start();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 防止主线程提前退出
        try {
            Thread.sleep(200000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void start() throws InterruptedException {
        int i = 0;
        for (LinkedList<Station> path : pathList) {
            // 每个工单生成一个小车
            Cart cart = new Cart();
            cart.setFullPath(path);

            Cart last = normalLine.getCarts().isEmpty() ? null : normalLine.getCarts().getLast();
            if (last == null) {
                cart.setHeader(true);
            }else{
                last.setNext(cart);
                cart.setPrev(last);
            }

            cart.setLine(normalLine);
            cart.setName(i++);
            cart.setAllStations(stations);

            normalLine.addCart(cart);

            cart.move();
            Thread.sleep(100);
        }
    }
}
